﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QunProject.Common;
using EnumHelper = QunProject.Model.EnumHelper;

namespace QunProject.Manage.Controllers
{
    /// <summary>
    /// 系统用户
    /// </summary>
    public class SysUserController : BaseController
    {
        // 首页
        public ActionResult Index()
        {
            return View();
        }
        // 编辑页
        public ActionResult Edit(int? id)
        {
            ViewBag.ID = id;
            return View();
        }
        // 获取数据
        public JsonResult AjaxData(int page = 1, int limit = 20, string qAccount = "", string qName = "")
        {
            var list = EF.sys_user.Where(x => true);
            if (!qAccount.IsNullOrWhiteSpace())
                list = list.Where(x => x.Account.Contains(qAccount));
            if (!qName.IsNullOrWhiteSpace())
                list = list.Where(x => x.Name.Contains(qName));



            var data = list.ToList().Skip((page - 1) * limit).Take(limit).Select(x => new
            {
                x.ID,
                x.Account,
                CreateTime = x.CreateTime.Value.ToString("yyyy-MM-dd hh:mm:ss"),
                x.IsValid,
                x.Name,
                x.Password,
                x.Remark
            });

            return Json(Result.SuccessReturnData(list.Count(), data));
        }
        // 提交编辑
        public JsonResult AjaxEdit(Model.sys_user model)
        {
            Model.sys_user _model = new Model.sys_user();
            if (!Common.Verify.VerifyLength(model.Account, 3, 16))
                return Json(Result.Fail("账号长度为3-16"));
            if (model.Name.IsNullOrWhiteSpace())
                return Json(Result.Fail("名称不能为空"));

            if (model.ID > 0)
            {
                _model = EF.sys_user.SingleOrDefault(x => x.ID == model.ID);
                _model = EF.sys_user.Attach(_model);
                _model.IsValid = model.IsValid;
                _model.Name = model.Name;
                _model.Remark = model.Remark;
                _model.Account = model.Account;
            }
            else
            {
                _model = EF.sys_user.SingleOrDefault(x => x.Account == model.Account);
                if (_model != null)
                    return Json(Result.Fail("已存在此用户名"));
                else
                    _model = new Model.sys_user();

                _model.CreateTime = DateTime.Now;
                _model.Account = model.Account;
                _model.IsValid = model.IsValid;
                _model.Name = model.Name;
                _model.Password = Common.Security.MD5Encrypt32(model.Password.IsNullOrWhiteSpace() ? UserDefaultPassword : model.Password);
                _model.Remark = model.Remark;
            }

            bool isSucc = false;
            if (model.ID > 0)
            {
                isSucc = EF.SaveChanges() > 0;
            }
            else
            {
                EF.sys_user.Add(_model);
                isSucc = EF.SaveChanges() > 0;
            }
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 提交删除
        public JsonResult AjaxDelete(string ids)
        {
            var delIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (delIds.Length == 0)
                return Json(Result.Fail("请选择需要删除的内容"));

            foreach (var id in delIds)
            {
                var modelId = id.ToInt();
                var model = EF.sys_user.SingleOrDefault(x => x.ID == modelId);
                model.IsValid = false;
            }

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 查询实体
        public JsonResult AjaxEntity(int id)
        {
            var model = EF.sys_user.SingleOrDefault(x => x.ID == id);
            if (model == null)
                return Json(Result.Fail("不存在此管理员"));
            return Json(Result.Success(model));
        }
    }
}